package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.BaseActivity;
import com.xmut.bs.handaccount.widget.ProgressWebView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

import static com.xmut.bs.handaccount.base.Constants.PARAM_ADV_ACTION;
import static com.xmut.bs.handaccount.base.Constants.PARAM_GOTO_HOME;


/**
 * Author Dollkey
 * Date 2017/12/28 16:20.
 */

public class AdvWebActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    @BindView(R.id.title_tv)
    TextView mTitleTv;
    @BindView(R.id.error_v)
    TextView mErrorView;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.web_view)
    ProgressWebView mWebView;
    @BindView(R.id.refresh_layout)
    BGARefreshLayout mRefreshLayout;

    private String advAction;
    private long dataId;
    private boolean isGotoHome;

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_adv_web;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mToolbar.setBackgroundColor(colorPrimary);
        mWebView.loadUrl(advAction);
        mWebView.setOnReceivedTitleListener(new ProgressWebView.OnReceivedTitleListener() {
            @Override
            public void onReceivedTitle(String title) {
                mTitleTv.setText(title);
            }

            @Override
            public void onReceivedError() {
                mErrorView.setText("请检查网络");
                mErrorView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageState(boolean isStart) {
                if (isStart) {
                    mErrorView.setText("");
                    mErrorView.setVisibility(View.VISIBLE);
                } else {
                    if(mErrorView.getText().toString().equals("")){
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mErrorView.setVisibility(View.GONE);
                            }
                        },500);
                    }
                }
            }
        });
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.mipmap.icon_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        initRefreshLayout(mRefreshLayout, this);
    }

    @Override
    protected void initParams(Intent intent) {
        advAction = intent.getStringExtra(PARAM_ADV_ACTION);
        isGotoHome = intent.getBooleanExtra(PARAM_GOTO_HOME, false);
        dataId = intent.getLongExtra("id", 0);
    }

    @OnClick({R.id.error_v, R.id.cancel_btn})
    public void onViewClicked(View view) {
        mErrorView.setText("");
        switch (view.getId()) {
            case R.id.error_v:
                mRefreshLayout.beginRefreshing();
                break;
            case R.id.cancel_btn:
                finish();
                break;
        }
    }

    private void goBack() {
        if (isGotoHome) {
            startActivity(new Intent(mContext, HomeActivity.class).putExtra("id", dataId));
            finish();
        } else {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack())
            mWebView.goBack();
        else
            goBack();
    }

    private void refresh() {
//        mErrorView.setVisibility(View.GONE);
        mWebView.clearHistory();
        mWebView.clearFormData();
        mContext.getCacheDir().delete();
        mContext.deleteDatabase("webview.db");
        mContext.deleteDatabase("webviewCache.db");
        mWebView.reload();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        refresh();
        mRefreshLayout.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
